<template>
    <div class="receive_header">
        <div class="title">
            <span>优惠券领取</span>
        </div>
    </div>
    <div class="receive_box">
        <div class="item" v-for="(item, index) in receiveList" :key="index">
            <div class="left">
                <div class="price">
                    <span class="amount">{{ item.rate }}</span>
                    <span class="symbol">折</span>
                </div>
            </div>
            <div class="right">
                <div class="info">
                    <div class="name">{{ item.name }}</div>
                    <div class="time">{{ item.createtime_date }} - {{ item.endtime_date }}</div>
                    <div class="desc">{{ item.description }}</div>
                </div>
                <div class="btn" @click="receiveCoupon(item, index)" :class="{ disabled: item.received }">
                    {{ item.received ? '已领取' : '立即领取' }}
                </div>
            </div>
        </div>
    </div>
  
    <Tabbar />
</template>
  
<script setup>
const { proxy } = getCurrentInstance()
const business = proxy.$business

// 领取列表数据
const receiveList = ref([])

// 获取领取列表
const getReceiveList = async () => {
    let params = {
        busid: business.value.id
    }

    let result = await proxy.$http.post({
        url: '/coupon/receive',
        params
    })

    if(result.code === 1) {
        receiveList.value = result.data.list
    }
}

// 领取优惠券
const receiveCoupon = async (item, index) => {
    if(item.received) {
        proxy.$fail('已领取过该优惠券')
        return
    }

    let params = {
        cid: item.id,
        busid: business.value.id
    }


    let result = await proxy.$http.post({
        url: '/coupon/receive',
        params
    })


    // 根据返回的msg判断是否成功
    if(result.code === 1) {
        proxy.$success({
            message: result.msg,
            onClose: () => {
                receiveList.value[index].received = true
            }
        })
    } else {
        proxy.$fail(result.msg)
    }
}

onBeforeMount(() => {
    getReceiveList()
})
</script>
  
<style scoped>
.receive_header {
    padding: 15px;
    background: #fff;
}

.receive_header .title {
    font-size: 18px;
    font-weight: bold;
    color: #333;
}

.receive_box {
    padding: 10px;
}

.receive_box .item {
    display: flex;
    background: #fff;
    border-radius: 8px;
    margin-bottom: 10px;
    padding: 15px;
    position: relative;
    overflow: hidden;
}

.receive_box .item::before {
    content: '';
    position: absolute;
    left: 0;
    top: 0;
    bottom: 0;
    width: 6px;
    background: #ff4444;
}

.receive_box .left {
    width: 120px;
    text-align: center;
    padding-right: 15px;
    border-right: 1px dashed #eee;
}

.receive_box .price {
    color: #ff4444;
    margin-bottom: 5px;
}

.receive_box .price .symbol {
    font-size: 14px;
}

.receive_box .price .amount {
    font-size: 28px;
    font-weight: bold;
}

.receive_box .condition {
    font-size: 12px;
    color: #999;
}

.receive_box .right {
    flex: 1;
    padding-left: 15px;
    display: flex;
    justify-content: space-between;
    align-items: center;
}

.receive_box .info {
    flex: 1;
}

.receive_box .name {
    font-size: 16px;
    color: #333;
    margin-bottom: 5px;
}

.receive_box .time {
    font-size: 12px;
    color: #999;
    margin-bottom: 5px;
}

.receive_box .desc {
    font-size: 12px;
    color: #666;
}

.receive_box .btn {
    width: 80px;
    height: 30px;
    line-height: 30px;
    text-align: center;
    background: #ff4444;
    color: #fff;
    border-radius: 15px;
    font-size: 14px;
}

.receive_box .btn.disabled {
    background: #ccc;
}
</style>